package com.liuya.safe.policy.controller;

import com.liuya.common.WebUtil;
import com.liuya.safe.model.SafeBusinessData;
import com.liuya.safe.policy.handler.BusinessDataPolicyDesignHandler;
import com.liuya.safe.policy.service.BusinessDataManagerService;
import io.swagger.annotations.Api;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.liuya.common.SpringUtils;
import com.liuya.safe.exception.AuthException;
import com.liuya.safe.policy.handler.AbstractPolicyDesignHandler;
import io.swagger.annotations.ApiOperation;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.StringReader;

/**
 * 查询策略测试
 */
@Controller
@Api(value = "查询策略", description = "查询策略")
@RequestMapping(value = "busiDataPolicyRaw")
public class BusinessDataPolicyRawController extends AbstractPolicyRawController {

    @ApiOperation(value = "跳转到手动模式主页面")
//    @RequiresPermissions("sys.busiDataPolicyRaw.editPolicy")
    @RequestMapping(value = "/editPolicy")
    public Object editPolicy(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.rawPolicy(req, rep, modelMap);
    }

    @ApiOperation(value = "跳转到手动模式主页面")
//    @RequiresPermissions("sys.queryPolicy.designVariables")
    @RequestMapping(value = "/designVariables")
    public Object designVariables(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.designVariables(req, rep, modelMap);
    }

    @ApiOperation(value = "跳转到手动模式主页面")
//    @RequiresPermissions("sys.queryPolicy.getVariable")
    @RequestMapping(value = "/getVariable")
    public Object getVariable(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.getVariable(req, rep, modelMap);
    }

    @ApiOperation(value = "跳转到手动模式主页面")
//    @RequiresPermissions("sys.queryPolicy.newVariable")
    @RequestMapping(value = "/newVariable")
    public Object newVariable(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.newVariable(req, rep, modelMap);
    }

    @ApiOperation(value = "跳转到手动模式主页面")
//    @RequiresPermissions("sys.queryPolicy.returnMain")
    @RequestMapping(value = "/returnMain")
    public Object returnMain(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.returnMain(req, rep, modelMap);
    }

    @ApiOperation(value = "跳转到测试主页面")
//    @RequiresPermissions("sys.queryPolicy.deleteVariable")
    @RequestMapping(value = "/deleteVariable")
    @ResponseBody
    public Object deleteVariable(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.deleteVariable(req, rep, modelMap);
    }

    @ApiOperation(value = "跳转到测试主页面")
//    @RequiresPermissions("sys.queryPolicy.addVariable")
    @RequestMapping(value = "/addVariable")
    @ResponseBody
    public Object addVariable(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.addVariable(req, rep, modelMap);
    }

    @ApiOperation(value = "跳转到测试主页面")
//    @RequiresPermissions("sys.queryPolicy.updateVariable")
    @RequestMapping(value = "/updateVariable")
    @ResponseBody
    public Object updateVariable(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.updateVariable(req, rep, modelMap);
    }

    @ApiOperation(value = "跳转到测试主页面")
//    @RequiresPermissions("sys.queryPolicy.save")
    @RequestMapping(value = "/save")
    @ResponseBody
    public Object save(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.save(req, rep, modelMap);
    }

    @ApiOperation(value = "跳转到测试主页面")
//    @RequiresPermissions("sys.queryPolicy.setRawScript")
    @RequestMapping(value = "/setRawScript")
    @ResponseBody
    public Object setRawScript(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        return super.setRawScript(req, rep, modelMap);
    }

    @RequestMapping(value = "/policyTest")
    public String policyTest(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        String id = WebUtil.getStringParameter(req, "id", "0");
        return "redirect:/busiDataPolicyTest/testPolicy.do?oper=loadFresh&id=" + id;
    }

    @RequestMapping(value = "/policyDesign")
    public String policyDesign(HttpServletRequest req, HttpServletResponse rep, ModelMap modelMap){
        String id = WebUtil.getStringParameter(req, "id", "0");
        return "redirect:/busiDataPolicyDesign/editPolicy?oper=loadFresh&id=" + id;
    }

    public String getPolicyHandlerAttributeKey(HttpServletRequest req) {
        int id = WebUtil.getIntParameter(req, "id", 0);
        return "_$safeBusinessData_" + id;
    }

    public AbstractPolicyDesignHandler createPolicyHandler(HttpServletRequest req) {
        String id = WebUtil.getStringParameter(req, "id", "0");

        BusinessDataManagerService manager = (BusinessDataManagerService) SpringUtils.getBean("businessDataManagerService");
        SafeBusinessData uc = manager.getBusinessData(id);

        com.liuya.db.sql.xml.BusinessDataType xmlUc;
        try {
            xmlUc = com.liuya.db.sql.xml.BusinessData.unmarshal(new StringReader(uc.getXmlContent()));
        } catch (Exception e) {
            throw new AuthException(e);
        }

//        return new BusinessDataPolicyDesignHandler(manager, xmlUc);
        return new BusinessDataPolicyDesignHandler(xmlUc);
    }

    protected void updateHandler(HttpServletRequest req, Object handler){
        String key = getPolicyHandlerAttributeKey(req);
        req.getSession().setAttribute(key, handler);
    }

}
